-
Notifications
You must be signed in to change notification settings - Fork 580
feat(integration): add gen_ai.conversation.id if available
#5307
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat(integration): add gen_ai.conversation.id if available
#5307
Conversation
…_ai.conversation.id`
Semver Impact of This PR🟡 Minor (new features) 📋 Changelog PreviewThis is how your changes will appear in the changelog. New Features ✨Span Streaming
Other
Bug Fixes 🐛Span Streaming
Other
Internal Changes 🔧Fastmcp
Mcp
Other
🤖 This preview updates automatically when you update the PR. |
…nts-conversation-id
Codecov Results 📊✅ 13 passed | Total: 13 | Pass Rate: 100% | Execution Time: 8.49s All tests are passing successfully. ❌ Patch coverage is 8.33%. Project has 13741 uncovered lines. Files with missing lines (180)
Generated by Codecov Action |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cursor Bugbot has reviewed your changes and found 3 potential issues.
Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.
| # Set conversation ID on workflow span early so it's captured even on errors | ||
| conversation_id = kwargs.get("conversation_id") | ||
| if conversation_id: | ||
| agent._sentry_conversation_id = conversation_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing conversation_id on workflow span in async runner
Medium Severity
In _create_run_wrapper, the comment says "Set conversation ID on workflow span early" but the code only stores conversation_id on the agent object (agent._sentry_conversation_id). Unlike the streaming version _create_run_streamed_wrapper, there's no call to set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conversation_id) on the workflow span itself. The workflow span (transaction) will be missing the gen_ai.conversation.id field when using Runner.run().
| if agent: | ||
| conv_id = getattr(agent, "_sentry_conversation_id", None) | ||
| if conv_id: | ||
| span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Callers don't pass agent to update_ai_client_span
Medium Severity
The update_ai_client_span function was modified to accept a new agent parameter (defaulting to None) for setting conversation_id on AI client spans. However, the callers in models.py at lines 128 and 179 were not updated to pass the agent argument. Since agent defaults to None, the conversation_id condition if agent: is never true, and gen_ai.conversation.id is never set on AI client spans.
| # Add conversation ID from agent | ||
| conv_id = getattr(agent, "_sentry_conversation_id", None) | ||
| if conv_id: | ||
| span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated pattern for setting conversation ID on spans
Low Severity
The same 3-line pattern for extracting and setting _sentry_conversation_id from an agent onto a span is repeated 4 times across execute_tool.py, handoff.py, invoke_agent.py, and ai_client.py. This pattern should be extracted to a utility function in utils.py (e.g., _set_conversation_id(span, agent)), following the existing pattern of _set_agent_data, _set_usage_data, etc.
| agent = args[0].clone() | ||
|
|
||
| with agent_workflow_span(agent): | ||
| # Set conversation ID on workflow span early so it's captured even on errors | ||
| conversation_id = kwargs.get("conversation_id") | ||
| if conversation_id: | ||
| agent._sentry_conversation_id = conversation_id |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The async wrapper _create_run_wrapper fails to set the conversation_id on the workflow span, unlike its synchronous counterpart, leading to missing telemetry data.
Severity: MEDIUM
Suggested Fix
Modify the _create_run_wrapper function to get a handle on the span created by agent_workflow_span. Then, call workflow_span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conversation_id) within the if conversation_id: block, similar to the implementation in the synchronous wrapper.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: sentry_sdk/integrations/openai_agents/patches/runner.py#L37-L43
Potential issue: In the async wrapper function `_create_run_wrapper`, the
`conversation_id` is set on `agent._sentry_conversation_id` but is not attached to the
workflow span itself. This is inconsistent with the synchronous wrapper
`_create_run_streamed_wrapper`, which explicitly calls `workflow_span.set_data()` to
attach the ID. The comment in the async function, "Set conversation ID on workflow span
early", indicates the intent was to set it on the span. As a result, when async agent
workflows are executed, the root workflow span will be missing the
`gen_ai.conversation.id` attribute, leading to inconsistent observability data between
async and sync execution paths.
Did we get this right? 👍 / 👎 to inform future reviews.
| agent = args[0].clone() | ||
|
|
||
| with agent_workflow_span(agent): | ||
| # Set conversation ID on workflow span early so it's captured even on errors | ||
| conversation_id = kwargs.get("conversation_id") | ||
| if conversation_id: | ||
| agent._sentry_conversation_id = conversation_id | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The async wrapper for _create_run_wrapper fails to set the conversation_id on the root workflow_span, unlike the sync implementation, causing inconsistent tracing data.
Severity: MEDIUM
Suggested Fix
In the async wrapper within _create_run_wrapper, after setting agent._sentry_conversation_id, add a call to workflow_span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conversation_id) to ensure the root span also has the conversation ID, matching the sync wrapper's behavior.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: sentry_sdk/integrations/openai_agents/patches/runner.py#L37-L44
Potential issue: In the async implementation of `_create_run_wrapper`, the
`conversation_id` is assigned to `agent._sentry_conversation_id`, but it is not set on
the root `workflow_span` itself. This is inconsistent with the sync/streaming
implementation, which does set the ID on the root span. As a result, the root
transaction span for asynchronous agent runs will be missing the
`gen_ai.conversation.id` attribute. This leads to an incomplete tracing experience, as
the comment "Set conversation ID on workflow span early" suggests the intention was to
capture it on the span.
Did we get this right? 👍 / 👎 to inform future reviews.


Description
Closes https://linear.app/getsentry/issue/TET-1721/openai-agents-add-gen-aiconversationid